<?php

/**
 * 推荐二维码
 */
namespace app\sale\api;

class QrcodeApi extends \app\member\api\MemberApi {

    public function index() {

        $userInfo = target('member/memberUser')->getInfo($this->userId);

        $saleInfo = target('sale/SaleUser')->getWhereInfo([
            'A.user_id' => $this->userId,
            'agent' => 1
        ]);

        $path = 'upload/qrcode/' . $this->userId . '/';
        $dir = ROOT_PATH . $path;
        if (!is_dir($dir)) {
            if (!\mkdir($dir, 0777, true)) {
                $this->error('upload目录没有写入权限！');
            }
        }

        $shareFile = $dir . 'share.jpg';
        $shareUrl = url('controller/member/login/index', ['sale_code' => $saleInfo['code']], true);


        $sharePath = ROOT_PATH . 'public/sale/images/share.png';
        $main = imagecreatefrompng ( $sharePath );
        $width = imagesx ( $main );


        $qrcodePath = $dir . "qrcode.png";
        \PHPQRCode\QRcode::png($shareUrl, $qrcodePath, 'L', 11, 1);

        //缩放二维码
        $dstCode = imagecreatefromstring(file_get_contents($qrcodePath));
        $qrcodeInfo = getimagesize($qrcodePath);
        $thumb = imagecreatetruecolor(185, 185);
        imagecopyresampled($thumb, $dstCode, 0, 0, 0, 0, 185, 185, $qrcodeInfo[0], $qrcodeInfo[1]);

        //写入二维码
        $dstShare = @imagecreatefromstring(file_get_contents($sharePath));
        imagecopy($dstShare, $thumb, 230, 230, 0, 0, 185, 185);
        imagedestroy($dstCode);
        imagedestroy($thumb);

        //写入头像
        if (!empty($userInfo['avatar'])) {
            $dstAvatar = imagecreatefromstring(file_get_contents($userInfo['avatar'] . '?type=3'));
            $avatarInfo = getimagesize($userInfo['avatar'] . '?type=3');
            $thumbAvatar = imagecreatetruecolor(115, 115);
            imagecopyresampled($thumbAvatar, $dstAvatar, 0, 0, 0, 0, 115, 115, $avatarInfo[0], $avatarInfo[1]);
            imagecopy($dstShare, $thumbAvatar, 265, 20, 0, 0, 115, 115);

            imagedestroy($dstAvatar);
        }

        //写入用户名
        $font = ROOT_PATH . 'public/common/fonts/adobehei.otf';

        $fontSize = 24;
        $fontColor = imagecolorallocate ( $dstShare, 255, 255, 255);
        $fontBox = imagettfbbox($fontSize, 0, $font, $userInfo['show_name']);
        imagettftext ( $dstShare, $fontSize, 0, ceil(($width - $fontBox[2]) / 2), 180, $fontColor, $font, $userInfo['show_name'] );


        //写入网站标题
        $fontSize = 16;
        $str = '我为' . $this->siteConfig['info_title'] . '代言';
        $fontColor = imagecolorallocate ( $dstShare, 255, 255, 255);
        $fontBox = imagettfbbox($fontSize, 0, $font, $str);
        imagefttext($dstShare, $fontSize, 0, ceil(($width - $fontBox[2]) / 2), 210, $fontColor, $font, $str);

        /*       //写入网站名称
               $white = imagecolorallocate($dstShare, 255, 255, 255);
               $text = '扫一扫，进入' . $this->siteConfig['info_name'] . '商城';
               $len = mb_strlen($text, "UTF-8");
               $left = (720 - $len * 32) / 2;
               imagefttext($dstShare, 30, 0, $left, 375, $white, $font, $text);*/

        //写入推荐码
        $fontSize = 20;
        $str = $saleInfo['code'];
        $fontColor = imagecolorallocate ( $dstShare, 0, 0, 0);
        $fontBox = imagettfbbox($fontSize, 0, $font, $str);
        imagefttext($dstShare, $fontSize, 0, ceil(($width - $fontBox[2]) / 2) + 40, 525, $fontColor, $font, $str);


        ob_start();//启用输出缓存，暂时将要输出的内容缓存起来
        imagejpeg($dstShare, null, 100);//输出
        $poster = ob_get_contents();//获取刚才获取的缓存
        ob_end_clean();//清空缓存
        imagedestroy($dstShare);

        file_put_contents($shareFile, $poster);

        $shareImg = DOMAIN . ROOT_URL . '/' . $path . 'share.jpg';

        $this->success('ok', [
            'share_img' => $shareImg,
            'share_url' => $shareUrl,
            'sale_code' => $saleInfo['code']
        ]);
    }


}